The NDK is a toolset that allows you to implement parts of your app using native-code languages such as C and C++. For certain types of apps, this can be helpful so you can reuse existing code libraries written in these languages, but most apps do not need the Android NDK.
Before downloading the NDK, you should understand that the NDK will not benefit most apps. As a developer, you need to balance its benefits against its drawbacks. Notably, using native code on Android generally does not result in a noticable performance improvement, but it always increases your app complexity. In general, you should only use the NDK if it is essential to your app—never because you simply prefer to program in C/C++.
#include"mraz_com_jnidemo_JniTest.h" #include<jni.h> // // Created by Mraz on 2016/6/28. // JNIEXPORT jstring JNICALL Java_mraz_com_jnidemo_JniTest_getHello(JNIEnv *env, jobject object) { return (*env)->NewStringUTF(env, "hello"); }
/* * Class: mraz_com_jnidemo_JniTest * Method: getSum * Signature: (II)I */ JNIEXPORT jint JNICALL Java_mraz_com_jnidemo_JniTest_getSum (JNIEnv *env, jobject object, jint a, jint b) { return (a + b); }
对应java类JniTest.java,加载so,定义native方法
1 2 3 4 5 6 7 8 9 10 11
package mraz.com.jnidemo; /** * Created by Mraz on 2016/6/28. */ publicclassJniTest{ static { System.loadLibrary("jniTest"); } publicnative String getHello(); publicnativeintgetSum(int a , int b); }